package org.lq.ht.view.goods;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.Document;

import org.lq.ht.entity.Dictionary;
import org.lq.ht.entity.Goods;
import org.lq.ht.entity.Supplier;
import org.lq.ht.entity.TypeID;
import org.lq.ht.service.DictionaryService;
import org.lq.ht.service.GoodsService;
import org.lq.ht.service.SupplierService;
import org.lq.ht.service.impl.DictionaryServiceImpl;
import org.lq.ht.service.impl.GoodsServiceImpl;
import org.lq.ht.service.impl.SupplierServiceImpl;
import org.lq.ht.util.CastUtils;
import org.lq.ht.util.PinYinUtil;
import org.lq.ht.util.ShowMessage;
import org.lq.ht.view.dictionary.V_dictionary;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.JButton;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JComboBox;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.ActionEvent;

public class goodsAdd extends JFrame {

	private JPanel contentPane;
	private JTextField txt_no;
	private JTextField txt_number;
	private JTextField txt_inventory;
	private JTextField txt_min_inventory;
	private JTextField txt_max_inventory;
	private JTextField txt_name;
	private JTextField txt_pinyin;
	private JTextField txt_purchase_price;
	private JTextField txt_sell_price;
	private JTextField txt_integral;
	private JComboBox cb_type;
	private JComboBox cb_unit;
	private JComboBox cb_supplier;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					goodsAdd frame = new goodsAdd();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public goodsAdd() {
		setTitle("商品添加");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 645, 422);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel = new JLabel("序号：");
		lblNewLabel.setBounds(30, 31, 58, 15);
		contentPane.add(lblNewLabel);
		
		txt_no = new JTextField();
		txt_no.setEnabled(false);
		txt_no.setEditable(false);
		txt_no.setBounds(98, 28, 58, 21);
		contentPane.add(txt_no);
		txt_no.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("编号：");
		lblNewLabel_1.setBounds(30, 69, 58, 15);
		contentPane.add(lblNewLabel_1);
		
		txt_number = new JTextField();
		txt_number.setColumns(10);
		txt_number.setBounds(98, 63, 109, 21);
		contentPane.add(txt_number);
		
		JLabel lblNewLabel_2 = new JLabel("类别：");
		lblNewLabel_2.setBounds(30, 105, 58, 15);
		contentPane.add(lblNewLabel_2);
		
		JLabel lblNewLabel_3 = new JLabel("库存：");
		lblNewLabel_3.setBounds(30, 180, 58, 15);
		contentPane.add(lblNewLabel_3);
		
		txt_inventory = new JTextField();
		txt_inventory.setColumns(10);
		txt_inventory.setBounds(98, 177, 109, 21);
		contentPane.add(txt_inventory);
		
		JLabel lblNewLabel_4 = new JLabel("最小库存：");
		lblNewLabel_4.setBounds(30, 221, 69, 15);
		contentPane.add(lblNewLabel_4);
		
		txt_min_inventory = new JTextField();
		txt_min_inventory.setColumns(10);
		txt_min_inventory.setBounds(98, 218, 109, 21);
		contentPane.add(txt_min_inventory);
		
		JLabel lblNewLabel_5 = new JLabel("最大库存：");
		lblNewLabel_5.setBounds(30, 264, 69, 15);
		contentPane.add(lblNewLabel_5);
		
		txt_max_inventory = new JTextField();
		txt_max_inventory.setColumns(10);
		txt_max_inventory.setBounds(98, 261, 109, 21);
		contentPane.add(txt_max_inventory);
		
		JLabel lblNewLabel_6 = new JLabel("名称：");
		lblNewLabel_6.setBounds(310, 31, 58, 15);
		contentPane.add(lblNewLabel_6);
		
		txt_name = new JTextField();
		//获取到当前文本框的对象
		Document doc = txt_name.getDocument();
		//添加内容改变事件
		doc.addDocumentListener(new DocumentAdapter() {//拼音监听窗口
			@Override
			public void insertUpdate(DocumentEvent e) {
				String py = PinYinUtil.getFirstPY(txt_name.getText());
				txt_pinyin.setText(py);
			}
			@Override
			public void removeUpdate(DocumentEvent e) {
				String py = PinYinUtil.getFirstPY(txt_name.getText());
				txt_pinyin.setText(py);
			}
		});
		txt_name.setColumns(10);
		txt_name.setBounds(378, 28, 109, 21);
		contentPane.add(txt_name);
		
		JLabel lblNewLabel_1_1 = new JLabel("拼音简码：");
		lblNewLabel_1_1.setBounds(310, 69, 75, 15);
		contentPane.add(lblNewLabel_1_1);
		
		txt_pinyin = new JTextField();
		txt_pinyin.setEnabled(false);
		txt_pinyin.setColumns(10);
		txt_pinyin.setBounds(378, 63, 109, 21);
		contentPane.add(txt_pinyin);
		
		JLabel lblNewLabel_2_1 = new JLabel("进价：");
		lblNewLabel_2_1.setBounds(310, 105, 58, 15);
		contentPane.add(lblNewLabel_2_1);
		
		txt_purchase_price = new JTextField();
		txt_purchase_price.setColumns(10);
		txt_purchase_price.setBounds(378, 102, 109, 21);
		contentPane.add(txt_purchase_price);
		
		JLabel lblNewLabel_3_1 = new JLabel("售价：");
		lblNewLabel_3_1.setBounds(310, 144, 58, 15);
		contentPane.add(lblNewLabel_3_1);
		
		txt_sell_price = new JTextField();
		txt_sell_price.setColumns(10);
		txt_sell_price.setBounds(378, 141, 109, 21);
		contentPane.add(txt_sell_price);
		
		JLabel lblNewLabel_4_1 = new JLabel("积分：");
		lblNewLabel_4_1.setBounds(310, 185, 58, 15);
		contentPane.add(lblNewLabel_4_1);
		
		txt_integral = new JTextField();
		txt_integral.setColumns(10);
		txt_integral.setBounds(378, 182, 109, 21);
		contentPane.add(txt_integral);
		
		JLabel lblNewLabel_5_1 = new JLabel("供应商：");
		lblNewLabel_5_1.setBounds(310, 228, 58, 15);
		contentPane.add(lblNewLabel_5_1);
		
		JLabel lblNewLabel_7 = new JLabel("备注：");
		lblNewLabel_7.setBounds(313, 267, 58, 15);
		contentPane.add(lblNewLabel_7);
		
		JTextPane txt_note = new JTextPane();
		txt_note.setBounds(377, 258, 146, 65);
		contentPane.add(txt_note);
		
		JButton btn_save = new JButton("保存");
		btn_save.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {//保存
				Goods g = new Goods();
				
				Dictionary type =  (Dictionary) cb_type.getSelectedItem();
				Dictionary unit =  (Dictionary) cb_unit.getSelectedItem();
				Supplier supplier= (Supplier) cb_supplier.getSelectedItem(); 
				System.out.println(unit);
				System.out.println(unit.getNo());
				g.setSupplier(supplier.getNo());
				g.setType(type.getNo());
				g.setUnit(unit.getNo());
				g.setNo(CastUtils.castInt(txt_no.getText()));
				g.setNumber(txt_number.getText());
				g.setInventory(CastUtils.castInt(txt_inventory.getText()));
				g.setMin_inventory(CastUtils.castInt(txt_min_inventory.getText()));
				g.setMax_inventory(CastUtils.castInt(txt_max_inventory.getText()));
				g.setName(txt_name.getText());
				g.setSpell(txt_pinyin.getText());
				g.setPurchase_price(CastUtils.castDouble(txt_purchase_price.getText()));
				g.setSell_price(CastUtils.castDouble(txt_sell_price.getText()));
				g.setIntegral(txt_integral.getText());
				g.setNote(txt_note.getText());
				
				GoodsService gs = new GoodsServiceImpl();
				ShowMessage.frameMessage(goodsAdd.this, gs.saveGoods(g), "保存");
				dispose();
				
			}
		});
		btn_save.setBounds(170, 350, 97, 23);
		contentPane.add(btn_save);
		
		JButton btnNewButton_1 = new JButton("取消");
		btnNewButton_1.setBounds(341, 350, 97, 23);
		contentPane.add(btnNewButton_1);
		
		JLabel lblNewLabel_8 = new JLabel("单位");
		lblNewLabel_8.setBounds(30, 144, 58, 15);
		contentPane.add(lblNewLabel_8);
		
		JLabel lblNewLabel_9 = new JLabel("");
		lblNewLabel_9.addMouseListener(new MouseAdapter() {//加号窗口操作
			@Override
			public void mouseClicked(MouseEvent e) {
				addCb();
			}
		});
		
		lblNewLabel_9.setIcon(new ImageIcon(goodsAdd.class.getResource("/images/add.png")));
		lblNewLabel_9.setBounds(217, 105, 39, 18);
		contentPane.add(lblNewLabel_9);
		
		JLabel lblNewLabel_9_1 = new JLabel("");
		lblNewLabel_9_1.addMouseListener(new MouseAdapter() {//加号窗口操作
			@Override
			public void mouseClicked(MouseEvent e) {
				addCb();
			}
		});
		lblNewLabel_9_1.setIcon(new ImageIcon(goodsAdd.class.getResource("/images/add.png")));
		lblNewLabel_9_1.setBounds(217, 149, 39, 18);
		contentPane.add(lblNewLabel_9_1);
		
		cb_type = new JComboBox();
		cb_type.setBounds(98, 101, 109, 23);
		contentPane.add(cb_type);
		
		cb_unit = new JComboBox();
		cb_unit.setBounds(98, 140, 109, 23);
		contentPane.add(cb_unit);
		
		cb_supplier = new JComboBox();
		cb_supplier.setBounds(378, 224, 109, 23);
		contentPane.add(cb_supplier);
		
		initCombox();
	}
	/**
	 * 初始化下拉列表
	 */
	public void initCombox() {
		DictionaryService ds = new DictionaryServiceImpl();
		List<Dictionary> typelist = ds.findById(TypeID.商品类型);
		List<Dictionary> unitlist = ds.findById(TypeID.单位);
		
		Dictionary def = new Dictionary();
		def.setNo(0);
		def.setName("--请选择--");
		def.setType_id(TypeID.默认);
		typelist.add(0,def);
		unitlist.add(0,def);
		//将指定的集合转换成数组
		DefaultComboBoxModel<Dictionary> typeDBM = new DefaultComboBoxModel<Dictionary>(typelist.toArray(new Dictionary[] {}));
		DefaultComboBoxModel<Dictionary> unitDBM = new DefaultComboBoxModel<Dictionary>(unitlist.toArray(new Dictionary[] {}));
		cb_type.setModel(typeDBM);
		cb_unit.setModel(unitDBM);
		//供应商下拉列表初始化
		SupplierService ss = new SupplierServiceImpl();
		List<Supplier>  supplierlist=   ss.findSupplierAll();
		Supplier s = new Supplier();
		s.setNo(-1);
		s.setName("--请选择--");
		supplierlist.add(0,s);
		//将指定的集合转换成数组
		DefaultComboBoxModel<Supplier> supplierDBM = new  DefaultComboBoxModel<Supplier>(supplierlist.toArray(new Supplier[] {}));
		cb_supplier.setModel(supplierDBM);
	}
	/**
	 * 为下拉列表中添加值
	 */
	public void addCb() {
		V_dictionary type = new V_dictionary();
		type.setBounds(goodsAdd.this.getX(), goodsAdd.this.getY(),type.getWidth(), type.getHeight());
		type.setVisible(true);
		type.addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosed(WindowEvent e) {
				initCombox();
			}
		});
		
		
		
		
		
	}
}
